package com.cloudz.iptv.utils.download;

import com.cloudz.iptv.api.v1.DownLoadFileServiceApi;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.Executors;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * @File Name: com.cloudz.iptv.utils.download
 * @Author: YJ //作者及
 * @Date: 2019/3/8 11:22 PM//完成日期
 * @Description: // 描述
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public class DownloadUtil
{

    public static void download(final String baseUrl, final String fileUrl, String filePath, final DownloadListener downloadListener)
    {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                //通过线程池获取一个线程，指定callback在子线程中运行。
                .callbackExecutor(Executors.newSingleThreadExecutor())
                .build();


        DownLoadFileServiceApi service = retrofit.create(DownLoadFileServiceApi.class);


        Call<ResponseBody> call = service.downloadApk(fileUrl);
        call.enqueue(new Callback<ResponseBody>()
        {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {

                //将Response写入到从磁盘中，详见下面分析
                //注意，这个方法是运行在子线程中的
                writeResponseToDisk(filePath, response, downloadListener);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                downloadListener.onFail("网络错误～");
            }
        });

    }

    private static void writeResponseToDisk(String path, Response<ResponseBody> response, DownloadListener downloadListener)
    {
        //从response获取输入流以及总大小
        writeFileFromIS(new File(path), response.body().byteStream(), response.body().contentLength(), downloadListener);
    }

    private static int sBufferSize = 8192;

    //将输入流写入文件
    private static void writeFileFromIS(File file, InputStream is, long totalLength, DownloadListener downloadListener)
    {
        //开始下载
        downloadListener.onStart();

        //创建文件
        if (!file.exists())
        {
            if (!file.getParentFile().exists())
                file.getParentFile().mkdir();
            try
            {
                file.createNewFile();
            } catch (IOException e)
            {
                e.printStackTrace();
                downloadListener.onFail("createNewFile IOException");
            }
        }
        OutputStream os = null;
        long currentLength = 0;
        try
        {
            os = new BufferedOutputStream(new FileOutputStream(file));
            byte data[] = new byte[sBufferSize];
            int len;
            while ((len = is.read(data, 0, sBufferSize)) != -1)
            {
                os.write(data, 0, len);
                currentLength += len;
                //计算当前下载进度
                downloadListener.onProgress(((float)currentLength / totalLength), totalLength);
            }
            //下载完成，并返回保存的文件路径
            downloadListener.onFinish(file.getAbsolutePath(), file);
        } catch (IOException e)
        {
            e.printStackTrace();
            downloadListener.onFail("IOException");
        } finally
        {
            try
            {
                is.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
            try
            {
                if (os != null)
                {
                    os.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
